<?php

class Ftl_Loader {

	const TYPE_LIBRARY		= 'library';
	const TYPE_CONTROLLER           = 'controller';

	private static $_CLASSES_MAP    = array
        (

            self::TYPE_LIBRARY  => array
                (
                    "prefix"        => "Ftl_",
                    "classes"       => array
                        (
                            "Log"           => "/",
                            "Mail"          => "/",

                            //Base de datos
                            "DB"            => "/",
                            "DataBase"      => "/DB/",
                            "MySql"         => "/DB/MySql/",
                            "MySqli"        => "/DB/MySqli/",
                            "PDO"           => "/DB/PDO/",

                            //Tipo de datos
                            "ArrayUtil"     => "/DataType/",
                            "StringUtil"    => "/DataType/",
                            "JsonUtil"      => "/DataType/",
                            "ObjectUtil"    => "/DataType/",

                            //Html
                            "Header"        => "/Html/",

                            //Encoding
                            "UTF8"          => "/Encoding/",
                            "QEncoding"     => "/Encoding/"



                        )
                )
            
        );

	public function __construct() {

		spl_autoload_register( array( $this, self::TYPE_LIBRARY ) );

	}

	public function library( $class ) {
		return $this->loadClass( $class, self::TYPE_LIBRARY );
	}

	private function loadClass( $class, $type ) {

		if( strpos($class, self::$_CLASSES_MAP[self::TYPE_LIBRARY]['prefix'] ) !== 0 ) {
			return false;
		}

		$path       = explode( '_', $class );
                $finalPath  = dirname( __FILE__ ).self::$_CLASSES_MAP[self::TYPE_LIBRARY]['classes'][$path[1]] . $path[1] . '.php';

                if( !file_exists( $finalPath ) )
                {
                    return false;
		}
                
		require_once $finalPath;

		return true;

	}


	private static $instance = null;

	public static function getInstance() {

        if( self::$instance === null ){
            self::$instance = new Ftl_Loader();
        }

        return self::$instance;

	}

}